package com.example.file_management.controller;

import com.example.file_management.pojo.user.Role;
import com.example.file_management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/role")
@CrossOrigin(origins = "http://localhost:5173")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 获取所有角色
    @GetMapping("/roles")
    public List<Role> getAllRoles() {
        return roleService.getAllRoles();
    }

    // 根据ID获取角色
    @GetMapping("/roles/{roleId}")
    public Role getRoleById(@PathVariable Long roleId) {
        return roleService.getRoleById(roleId);
    }

    // 创建角色
    @PostMapping("/roles")
    public Long createRole(@RequestBody Role role) {
        return roleService.createRole(role);
    }

    // 更新角色
    @PutMapping("/roles/{roleId}")
    public String updateRole(@PathVariable Long roleId, @RequestBody Role role) {
        role.setRoleId(roleId);
        return roleService.updateRole(role);
    }

    // 删除角色
    @DeleteMapping("/roles/{roleId}")
    public String deleteRole(@PathVariable Long roleId) {
        return roleService.deleteRole(roleId);
    }
}